package com.study.designpattern.factory;

public class FactoryMethod {
    public static void main(String[] args) {
//        Application app=new Application();
//        Product product=app.getObject(1);
//        product.method1();
        Application1 app=FactoryProducer.getFactory("A");
        Product product=app.getObject();
        product.method1();
    }
}
interface Product{
    public void method1();
}
class ProductA implements Product{
public void method1(){
    System.out.println("======>ProductA.method1 executed");
}
}
class ProductB implements Product{
    public void method1(){
        System.out.println("======>ProductB.method1 executed");
    }
}
//简单工厂，并不是设计模式，而是一种设计方法，使代码更灵活，增加可用性。
class SimpleFactory{
    public static Product createProduct(int type){
        switch (type){
            case 0:
                return new ProductA();
            case 1:
                return new ProductB();
            default:
                return null;
        }
    }
}
class Application{
    private Product createProduct(int type){

        return SimpleFactory.createProduct(type);
    }
        public Product getObject(int type){
        Product product=createProduct(type);
        return product;
    }
}
//工厂方法模式，符合开闭原则，单一职责原则
abstract class Application1{
    abstract Product createProduct();
    public Product getObject(){
        Product product=createProduct();
        return product;
    }
}
class conCreateProductA extends Application1{
    @Override
    Product createProduct() {
        return new ProductA();
    }
}
class conCreateProductB extends Application1{
    @Override
    Product createProduct() {
        return new ProductB();
    }
}
class FactoryProducer{
    public static Application1 getFactory(String type){
        switch (type){
            case "A":
                return new conCreateProductA();
            case "B":
                return new conCreateProductB();
            default:
                return null;
        }
    }
}
